﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Numerics;
using LuaInterface;

namespace MathScript
{
    public partial class Form1 : Form
    {
        Lua lua = new Lua();
        public class StrFunc
        {
            public RichTextBox richConsole;
            

            public StrFunc(RichTextBox rich)
            {
                richConsole = rich;
            }

            public void teste(LuaFunction func) //LuaTable table)
            {
                MessageBox.Show(func.ToString());
                //foreach (DictionaryEntry dd in table)
                //{
                //    MessageBox.Show(dd.Value.ToString());
                //}
            }

            // Função para contar quantidade de certa string contida numa string
            public int CountStringOccurrences(string text, string pattern)
            {
                // Loop through all instances of the string 'text'.
                int count = 0;
                int i = 0;
                while ((i = text.IndexOf(pattern, i)) != -1)
                {
                    i += pattern.Length;
                    count++;
                }
                return count;
            }
            
            // Função privada para o comando de derivada
            private string[] getcharpot(string s)
            {
                string[] num = new string[] { "", "" };
                int n = s.Length;
                for(int i=0; i<n; i++)
                {
                    if(Char.IsNumber(Convert.ToChar(s.Substring(i, 1))))
                    {
                        num[0] += s.Substring(i, 1);
                    }
                    else
                    {
                        num[1] += s.Substring(i, 1);
                    }
                }

                return num;
            }

            public string derivative(string s)
            {
                if (CountStringOccurrences(s, "x") > 0)
                {
                    if (CountStringOccurrences(s, "^") > 0)
                    {
                        char[] delimiters = new char[] { 'x', '^' };
                        string[] e = s.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                        if (e[1] == "0")
                            return "";
                        if (e[0] == "")
                            e[0] = "1";
                        if (e[0] == "-")
                            e[0] = "-1";

                        string[] bas, exp;
                        bas = getcharpot(e[0]);
                        exp = getcharpot(e[1]);

                        if (exp[0] == "")
                            exp[0] = "1";

                        if (exp[1] == "")
                            exp[1] = "0";

                        if (bas[0] == "")
                            bas[0] = "1";

                        if (bas[1] == "")
                            bas[1] = "0";

                        string opfrac;
                        string multfrac = Convert.ToString(Convert.ToInt16(bas[0]) * Convert.ToInt16(exp[0]) + Convert.ToInt16(bas[1]) + Convert.ToInt16(exp[1]));
                        if (Char.IsNumber(Convert.ToChar(e[1])))
                        {
                            opfrac = Convert.ToString(Convert.ToInt16(e[1]) - 1);
                        }
                        else
                        {
                            opfrac = "(" + e[1] + "-1)";
                        }

                        if (opfrac == "1")
                        {
                            richConsole.Text += multfrac + "x" + "\n";
                            return multfrac + "x";
                        }
                        if (opfrac == "0")
                        {
                            return multfrac;
                        }
                        else
                        {
                            richConsole.Text += multfrac + "x^" + opfrac + "\n";
                            return multfrac + "x^" + opfrac;
                        }
                    }
                    else
                    {
                        string[] e = s.Split("x".ToCharArray());
                        if (e[0] == "" && e[0] == "-")
                        {
                            return s.Replace("x", "1");
                        }
                        else
                        {
                            return s.Replace("x", "");
                        }
                    }
                }
                else
                {
                    return "";
                }
            }

            public void print(string s)
            {
                //MessageBox.Show(s);
                richConsole.Text += s + "\n";
            }

            public void clear()
            {
                richConsole.Text = "";
            }
        }

        public Form1()
        {
            InitializeComponent();
            //string text = "derivative(xx)";
            //text = Regex.Replace(text, "derivative\\((.+?)\\)", "derivative('$1')");
            //MessageBox.Show(text);
            //string Command;
            string p = "212121212212121212121212121212121212121212121212121212121212121212121212123456212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212163";
            BigInteger number = BigInteger.Subtract(BigInteger.Parse(p), BigInteger.Parse("1"));
            number++;
            MessageBox.Show(number.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            StrFunc strfunc = new StrFunc(richTextBox1);
            //Form1 fom = new Form1();
            //lua.RegisterFunction("DanSays", fom, fom.GetType().GetMethod("DanSays"));
            lua.RegisterFunction("derivative", strfunc, strfunc.GetType().GetMethod("derivative"));
            lua.RegisterFunction("print", strfunc, strfunc.GetType().GetMethod("print"));
            lua.RegisterFunction("clear", strfunc, strfunc.GetType().GetMethod("clear"));
            lua.RegisterFunction("teste", strfunc, strfunc.GetType().GetMethod("teste"));
            //richTextBox1.Text = strfunc
            try
            {
                lua["myvar"] = "a";
                lua["testando[1]"] = 1;
                lua.DoString(richTextBox2.Text);
                //MessageBox.Show(a[0].ToString());
                //richTextBox1.Text += lua.DoString(richTextBox2.Text).ToString();

            }
            catch (Exception ex)
            {
                richTextBox1.Text += ex.Message.ToString() + "\n";
            }

            richTextBox1.Text += "\n ";
        }


    }
}
